home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.4 Applications 1997 August / SGI IRIX 6.4 Applications 1997 August.iso / dist / gateway.idb / usr / WebFace / Source / 20-NetworkServices / dhcp / dhcp-sopts.frm.z / dhcp-sopts.frm
Encoding:
Text File  |  1997-07-30  |  10.9 KB  |  348 lines

  1. #!/usr/bin/perl5
  2. #
  3. # dhcp-sopts.cgi
  4. #
  5. # Copyright 1988-1996 Silicon Graphics, Inc.
  6. # All rights reserved.
  7. #
  8. # This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  9. # the contents of this file may not be disclosed to third parties, copied or
  10. # duplicated in any form, in whole or in part, without the prior written
  11. # permission of Silicon Graphics, Inc.
  12. #
  13. # RESTRICTED RIGHTS LEGEND:
  14. # Use, duplication or disclosure by the Government is subject to restrictions
  15. # as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  16. # and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  17. # successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  18. # rights reserved under the Copyright Laws of the United States.
  19. #
  20. # $Id: dhcp-sopts.frm,v 1.28 1997/04/17 21:23:07 shotes Exp $
  21.  
  22. use Sys::Hostname;
  23. BEGIN { require "/usr/WebFace/lib/CGI.pm"; import CGI; }
  24. require "/usr/OnRamp/lib/OnRamp.pm";
  25.  
  26. $query = new CGI;
  27.  
  28. $title = "DHCP Server Setup";
  29.  
  30. $js =
  31. "which = \"none\";
  32. function runSubmit() {
  33.     // onClick gets processed before onSubmit
  34.     if(which == \"ok\") return runOK();
  35.     return (true);
  36. }
  37. function markOK() { which = \"ok\"; }
  38. function markOther() { which = \"none\"; }
  39. function runOK() {
  40.     form = document.DHCPForm;
  41.     if (!testFilename(form.hosts_map)) return (false);
  42.     if (!testFilename(form.ethers_map)) return (false);
  43.     return (true);
  44. }
  45. function testFilename(Ctrl) {
  46.     word = Ctrl.value;
  47.     if (word == \"\") { errorBox (Ctrl, \"The filename is required.\"); return (false); }
  48.     if (!testMetaChars(word)) { errorBox (Ctrl, \"Filename \" + word + \" cannot \\ncontain \" + illegal + \" characters.\"); return (false); }
  49.     for(i = 0; i < word.length; i++) {
  50.         if (word.charAt(i) == ' ') { errorBox (Ctrl, \"Filename \" + word + \" contains spaces.\"); return (false); }
  51.     }
  52.     if (word.charAt(0) != '/') { errorBox (Ctrl, \"Filename \" + word + \" must be fully qualified.\"); return (false); }
  53.     if (!testBadNames(word)) { errorBox (Ctrl, word + \" is a reserved directory.\"); return (false); }
  54.     return (true);
  55. }
  56. function testMetaChars(word) {
  57.     metaChars = \"{}()*!~<>?|[]'&^\$\@\#`\\\";:\";
  58.     for(j = 0; j < metaChars.length; j++) {
  59.         illegal = metaChars.charAt(j);
  60.         if (word.indexOf (illegal, 0) != -1) return (false);
  61.     } return (true);
  62. }
  63. function testBadNames(word) {
  64.     if (word == \"/\" || word == \"/usr\" || word == \"/etc\" || word == \"/bin\" || word == \"/sbin\" || word == \"/usr\" || word == \"/usr/sbin\") return (false);
  65.     return (true);
  66. }
  67. function errorBox (Ctrl, ErrorMessage) {
  68.     alert (ErrorMessage);  Ctrl.focus();  return;
  69. }";
  70.  
  71.  
  72. print $query->header;
  73.  
  74. $help = $document_root . $ENV{"SCRIPT_NAME"};
  75. $help =~ s/cgi$/hlp/;
  76. exec $help if ($query->param('help') eq "Help");
  77.  
  78. &js_title_block($title,$js);
  79.  
  80. $conf = "/etc/inetd.conf";
  81. $osf = $def_osf = "/etc/config/dhcp_bootp.options";
  82.  
  83. $dummy = "/etc/inetd.conf.tmp";
  84.  
  85. $def_hosts = "/etc/hosts";
  86. $def_ethers = "/etc/ethers";
  87. $def_sys = "/unix";
  88. $def_propel_dbm = "/var/IRIXpro";
  89. $def_config_dir = "/var/dhcp/config";
  90. $host = hostname;
  91. $def_propel_mgr = "$host:1525";
  92. $dbfile = "/usr/IRIXpro/lib/system.dbtclrc";
  93. $use_propel = "No";
  94.  
  95. $rly_off = system ("/etc/chkconfig", "proclaim_relayagent");
  96. $srv_off = system ("/etc/chkconfig", "proclaim_server");
  97. $def_sstate = "No";
  98. if ($srv_off == 0) {
  99.     $def_sstate = "Yes";
  100. }
  101.  
  102. if ($def_sstate eq "Yes") { &getFilename; }
  103.  
  104. if ($query->param) {
  105.     &formValidation;
  106.     if ($query->param('doit') eq 'Ok') {    
  107.         $new_sstate = $query->param('sstate');
  108.         &error("You must first disable DHCP relay agent before you can
  109.             enable DHCP server.") if ($new_sstate eq "Yes" && !$rly_off);
  110.         if ($new_sstate ne $def_sstate) {            # state changed
  111.             if ($new_sstate eq "Yes") {
  112.                 system ("/etc/chkconfig", "autoconfig_ipaddress", "off");
  113.                 system ("/etc/chkconfig", "proclaim_server", "on");
  114.             } else {
  115.                 system ("/etc/chkconfig", "proclaim_server", "off"); 
  116.             }
  117.         }
  118.         if ($new_sstate eq 'Yes') { &makeChanges; }
  119.     }
  120.     print "<i>The new state has been saved.</i>\n";
  121. }
  122.  
  123. if ($def_sstate eq "Yes") { &readConfig; }
  124.  
  125. &generic;
  126.  
  127. sub formValidation {
  128.     my $error = &check_fname($query->param('hosts_map'), 1);
  129.     &error($error) if $error;    
  130.     
  131.     $error = &check_fname($query->param('ethers_map'), 1);
  132.     &error($error) if $error;
  133. }
  134.  
  135. sub error {
  136.     &error_block($_[0]);
  137.     &generic;
  138.     exit 0;
  139. }
  140.  
  141. sub makeChanges {
  142. #    if ($query->param('opt_file') ne $osf) {
  143. #        $osf = $query->param('opt_file');
  144. #        $value = "bootp\tdgram\tudp\twait\troot\t/usr/etc/bootp\tbootp\t-o $osf\n";
  145. #        &putEntry($conf,"bootp",$value); 
  146. #    }
  147.  
  148.     $rename = 0;
  149.     open(IN,"< $conf");
  150.     open(OUT,"> $dummy");
  151.     while(<IN>) {
  152.         @items = split(/\s+/);
  153.         if ($items[6] eq "bootp" || $items[7] eq "bootp") {
  154.         if ($items[0] eq "bootp") {
  155.             print OUT "# $_";
  156.             $rename = 1;
  157.         }
  158.         }
  159.         elsif($items[6] eq "dhcp_bootp" || $items[7] eq "dhcp_bootp") {
  160.         if ($items[0] ne "bootp") {
  161.             $line = "bootp\tdgram\tudp\twait\troot\t/usr/etc/dhcp_bootp ";
  162.             $line .= "dhcp_bootp -o /etc/config/dhcp_bootp.options\n";
  163.             print OUT $line;
  164.             $rename = 1;
  165.         }
  166.         }
  167.         else { print OUT $_; }
  168.     }
  169.     close(IN);
  170.     close(OUT);
  171.     if ($rename) { rename($dummy,$conf); }        
  172.  
  173.     open(OUT,"> $osf");
  174.  
  175.     if ($query->param('use_propel') eq 'Yes') {
  176.         print OUT "-s ", $query->param('propel_dbms'), "\n";
  177.         if ($query->param('propel_mgr') ne $def_propel_mgr) {
  178.             print OUT "ih ", $query->param('propel_mgr'), "\n";
  179.         }
  180.     }
  181.     if ($query->param('hosts_map') ne '/etc/hosts') {
  182.         print OUT "-w ", $query->param('hosts_map'), "\n"; }
  183.     if ($query->param('ethers_map') ne '/etc/ethers') {
  184.         print OUT "-e ", $query->param('ethers_map'), "\n"; }
  185. #    if ($query->param('sys_name') ne '/unix') {
  186. #        print OUT "-u ", $query->param('sys_name'), "\n"; }
  187. #    if ($query->param('config_dir') ne '/var/dhcp/config') {
  188. #        print OUT "-c ", $query->param('config_dir'), "\n"; }
  189.     close(OUT);
  190. }
  191.  
  192. sub putEntry { 
  193.     local($file) = $_[0];
  194.     local($key) = $_[1];
  195.     local($val) = $_[2];
  196.     local($dummy) = "dummy";
  197.  
  198.     open(IN,"< $file");
  199.     open(OUT,"> $dummy");
  200.     local($found) = 0;
  201.     while(<IN>) {
  202.         if (/^$key/ || /^#\s*$key/) {
  203.             print OUT $val,"\n";
  204.             $found = 1;
  205.         } else { print OUT $_; }
  206.     }
  207.     if ($found == 0) { print OUT $val,"\n"; }
  208.     rename($dummy, $file) || &error("Unable to rename $dummy to $file");
  209.     close(IN);
  210.     close(OUT);
  211. }
  212.  
  213. sub getFilename {
  214.     open(INETD, $conf) || &error("Unable to open $conf");
  215.     $osf = "";
  216.     while (<INETD>) {
  217.     if (/^#/) {
  218.         next;
  219.     }
  220.     # bootp dgram udp wait root /usr/etc/dhcp_bootp dhcp_bootp -o <file>
  221.     if (/^bootp/) {
  222.         chop;
  223.         @entlist = split(/\s+/);
  224.         if (($entlist[6] eq "dhcp_bootp") && ($entlist[7] eq "-o")) {
  225.         $osf = $entlist[8];
  226.         last;    # Break out of the while loop
  227.         }
  228.     }
  229.     }
  230.     close(INETD);
  231.     if (!$osf) { $osf = $def_osf; }
  232. }
  233.  
  234. sub readConfig {
  235.     open(OPTIONS, $osf) || &error("Unable to open $osf");
  236.     while(<OPTIONS>) {
  237.     if (/^#/) {
  238.         next;
  239.     }
  240.     chop;
  241.     @optlist = split(/\s+/);
  242.     $i = 0;
  243.     while ($optlist[$i]) {
  244.         if ($optlist[$i] eq "-y") {
  245.         &error("Option $optlist[$i] in file $osf is no longer supported");
  246.         }
  247.         elsif ($optlist[$i] eq "-s") {
  248.         $def_propel_dbm = $optlist[$i+1];
  249.         $use_propel = "Yes";
  250.         $i += 2;
  251.         next;
  252.         }
  253.         elsif ($optlist[$i] eq "-h") {
  254.         $def_propel_mgr  = $optlist[$i+1];
  255.         $i += 2;
  256.         next;
  257.         }
  258.         elsif ($optlist[$i] eq "-w") {
  259.         $def_hosts = $optlist[$i+1];
  260.         $i += 2;
  261.         next;
  262.         }
  263.         elsif ($optlist[$i] eq "-e") {
  264.         $def_ethers = $optlist[$i+1];
  265.         $i += 2;
  266.         next;
  267.         }
  268.         elsif ($optlist[$i] eq "-u") {
  269.         $def_sys = $optlist[$i+1];
  270.         $i += 2;
  271.         next;
  272.         }
  273.         elsif ($optlist[$i] eq "-c") {
  274.         $def_config_dir = $optlist[$i+1];
  275.         $i += 2;
  276.         next;
  277.         }
  278.         else {
  279.         &error("Option $optlist[ei] in file $osf is not recognized");
  280.         }
  281.         $i += 1;
  282.     }
  283.     }
  284.     close(OPTIONS);
  285. }
  286.  
  287. sub generic {
  288.     &header_block($title);
  289.  
  290.     print $query->startform("POST", "", "", "NAME=DHCPForm", "onSubmit=\"return runSubmit()\"");
  291.  
  292.     print "<center ><table cellpadding=5 width=450>\n";
  293.  
  294.     print "<tr><th align=left>Enable DHCP server:</th><th align=left>\n",
  295.               $query->radio_group(-name=>'sstate',
  296.               -values=>['Yes','No'],
  297.               -default=>$def_sstate), "</th></tr>";
  298.  
  299. #    print "<tr><th align=left>DHCP Server Option File:</th><th align=left>\n",
  300. #              $query->textfield(-name=>'opt_file',
  301. #            -default=>$osf,
  302. #            -size=>20, -maxlength=>256), "</th></tr>";
  303.  
  304.     if (-e $dbfile) {
  305.             print "<hr><tr><th align=left>Use the Propel database for the hosts 
  306.                 and ethers maps:</th><th align=left>\n",
  307.               $query->radio_group(-name=>'use_propel',
  308.                   -values=>['Yes','No'],
  309.                   -default=>$use_propel), "</th></tr>";
  310.             print "<tr><th align=left>Directory for Propel database:</th><th align=left>\n",
  311.               $query->textfield(-name=>'propel_dbms',
  312.                 -default=>$def_propel_dbm,
  313.                 -size=>20, -maxlength=>256), "</th></tr>";
  314.             print "<tr><th align=left>Hostname of Propel lock manager:</th><th align=left>\n",
  315.               $query->textfield(-name=>'propel_mgr',
  316.                 -default=>$def_propel_mgr,
  317.                 -size=>20, -maxlength=>256), "</th></tr>";
  318.     }
  319.  
  320.     print "<tr><th align=left>File name for translating IP addresses to 
  321.             hostnames:</th><th align=left>\n",
  322.               $query->textfield(-name=>'hosts_map',
  323.             -default=>$def_hosts,
  324.             -size=>20, -maxlength=>256), "</th></tr>";
  325.     print "<tr><th align=left>File name for translating ethernet address to 
  326.             hostnames:</th><th align=left>\n",
  327.               $query->textfield(-name=>'ethers_map',
  328.             -default=>$def_ethers,
  329.             -size=>20, -maxlength=>256), "</th></tr>";
  330.  
  331. #    print "<tr><th align=left>System File Name:</th><th align=left>\n",
  332. #              $query->textfield(-name=>'sys_name',
  333. #            -default=>$def_sys,
  334. #            -size=>20, -maxlength=>256), "</th></tr>";
  335. #    print "<tr><th align=left>DHCP Config Directory:</th><th align=left>\n",
  336. #              $query->textfield(-name=>'config_dir',
  337. #            -default=>$def_config_dir,
  338. #            -size=>20, -maxlength=>256), "</th></tr>";
  339.  
  340.     print "</table></center><br>";
  341.  
  342.     print &js_buttons('doit','Ok','onClick="markOK()"','onClick="markOther()"');
  343.  
  344.     print $query->endform;
  345.  
  346.     print $query->end_html;
  347. }
  348.